package com.zrrd.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrrd.reggie.bean.OrderDetail;
import com.zrrd.reggie.bean.Orders;
import com.zrrd.reggie.bean.dto.OrderDto;
import com.zrrd.reggie.service.OrderDetailService;
import com.zrrd.reggie.service.OrdersService;
import com.zrrd.reggie.util.BaseContext;
import com.zrrd.reggie.util.CustException;
import com.zrrd.reggie.util.R;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/order")
public class OrdersController {
    //不赖不赖 真不赖
    //order业务层对象
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private OrderDetailService detailService;

    @GetMapping("/page")
    public R<Page> page(Integer page, Integer pageSize, String number, String beginTime, String endTime) {
        Page<Orders> pageInfo = new Page<>(page, pageSize);
        QueryWrapper<Orders> wrapper = new QueryWrapper<>();
        if (number != null && !number.equals("")) {
            wrapper.eq("number", number);
        }
        if (beginTime != null && !beginTime.equals("") && endTime != null && !endTime.equals(endTime)) {
            wrapper.between("order_time", beginTime, endTime);
        }
        ordersService.page(pageInfo, wrapper);
        return R.sucess(pageInfo);
    }

    @PutMapping
    public R<String> updateStatus(@RequestBody Orders orders) {
        return ordersService.updateById(orders) ? R.sucess("修改成功") : R.error("修改失败");
    }

    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders) throws CustException {
        ordersService.saveOrder(orders);
        return R.sucess("添加成功");
    }

    @GetMapping("/userPage")
    public R<Page> userPage(Integer page, Integer pageSize) {
        Page<Orders> pageInfo = new Page<>(page, pageSize);
        QueryWrapper<Orders> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", BaseContext.getCurrentId());
        wrapper.orderByDesc("order_time");
        ordersService.page(pageInfo, wrapper);
        Page<OrderDto> dtoPage = new Page<>();
        BeanUtils.copyProperties(pageInfo, dtoPage, "records");
        List<OrderDto> dtoList = new ArrayList<>();
        List<Orders> orders = pageInfo.getRecords();
        for (Orders order : orders) {
            OrderDto dto = new OrderDto();
            BeanUtils.copyProperties(order, dto);
            QueryWrapper<OrderDetail> detailQueryWrapper = new QueryWrapper<>();
            detailQueryWrapper.eq("order_id", order.getId());
            List<OrderDetail> detailList = detailService.list(detailQueryWrapper);
            dto.setOrderDetails(detailList);
            dtoList.add(dto);
        }
        dtoPage.setRecords(dtoList);
        return R.sucess(dtoPage);
    }
    @PostMapping("/again")
    public R again(@RequestBody Orders orders){
        ordersService.again(orders);
        return R.sucess("再来一单成功");
    }
}